/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import entity.Contact;
import entity.Invitation;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import entity.Event;
import entity.RegisteredUser;
import entity.enumeration.InvitationStatus;

/**
 *
 * @author Stefan
 */
public class InvitationManager extends DefaultManager<Invitation> {

    public InvitationManager() {
        super(Invitation.class);
    }

    public List<Invitation> getInvitationsForEvent(Event selectedEvent) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Invitation x WHERE x.event = ?1 AND x.deleted = 0 ";
        Query q = em.createQuery(query);
        q.setParameter(1, selectedEvent);
        List<Invitation> invitations = q.getResultList();

        em.close();
        return invitations;
    }

    public List<Invitation> getAnswerdInvitationsForEvent(Event selectedEvent) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Invitation x WHERE x.event = ?1 AND x.deleted = 0 AND (x.status = ?2 OR x.status = ?3)";
        Query q = em.createQuery(query);
        q.setParameter(1, selectedEvent);
        q.setParameter(2, InvitationStatus.Attending);
        q.setParameter(3, InvitationStatus.Not_Attending);
        List<Invitation> invitations = q.getResultList();

        em.close();
        return invitations;
    }

    public List<Invitation> getUserInvitations(RegisteredUser ru) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Invitation x WHERE x.contact.email = ?1 AND x.deleted = 0 ";
        Query q = em.createQuery(query);
        q.setParameter(1, ru.getEmail());
        List<Invitation> invitations = q.getResultList();

        em.close();
        return invitations;
    }

    public Long getContactIdForEvent(RegisteredUser ru, Event ev) {
        EntityManager em = getEntityManager();
        String query = "SELECT x.contact.id FROM Invitation x WHERE x.contact.email = ?1 AND x.event= ?2 AND x.deleted = 0 ";
        Query q = em.createQuery(query);
        q.setParameter(1, ru.getEmail());
        q.setParameter(2, ev);
        List<Long> invitations = q.getResultList();
        if (invitations.isEmpty()) {
            return null;
        }

        em.close();
        return invitations.get(0);
    }

    public Invitation getInvitationForEvent(Contact co, Event ev) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Invitation x WHERE x.contact = ?1 AND x.event= ?2 AND x.deleted = 0 ";
        Query q = em.createQuery(query);
        q.setParameter(1, co);
        q.setParameter(2, ev);
        List<Invitation> invitations = q.getResultList();
        if (invitations.isEmpty()) {
            return null;
        }

        em.close();
        return invitations.get(0);
    }

    public Contact getContactForEvent(RegisteredUser user, Event selectedEvent) {
        EntityManager em = getEntityManager();
        String query = "SELECT x.contact FROM Invitation x WHERE x.contact.email = ?1 AND x.event= ?2 AND x.deleted = 0 ";
        Query q = em.createQuery(query);
        q.setParameter(1, user.getEmail());
        q.setParameter(2, selectedEvent);
        List<Contact> invitations = q.getResultList();
        if (invitations.isEmpty()) {
            return null;
        }

        em.close();
        return invitations.get(0);
    }
}
